package recite.word.service.base.common.config;

import cn.hutool.core.collection.CollUtil;
import com.aliyun.alimt20181012.Client;
import com.aliyun.teaopenapi.models.Config;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.region.Region;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @program: recite-word-back
 * @description:
 * @author: Mr.lin
 * @create: 2023-01-28 11:58
 **/
@Configuration
public class CommonConfig {
    /**
     * 序列号配置
     *
     * @return
     */
    @Bean
    public WebMvcConfigurer longToStringConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
                if (CollUtil.isNotEmpty(converters)) {
                    for (HttpMessageConverter<?> converter : converters) {
                        if (converter instanceof MappingJackson2HttpMessageConverter) {
                            MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = (MappingJackson2HttpMessageConverter) converter;
                            ObjectMapper objectMapper = jackson2HttpMessageConverter.getObjectMapper();
                            //不显示为null的字段
//							objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
                            SimpleModule simpleModule = new SimpleModule();
                            simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
                            simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
                            objectMapper.registerModule(simpleModule);
                            // 时间格式化
//							objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//							objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

                            jackson2HttpMessageConverter.setObjectMapper(objectMapper);
                        }
                    }
                }
            }
        };
    }

    /**
     * 腾讯对象存储
     * @return
     */
    @Bean
    public COSClient myCosClient() {
        // 1 初始化用户身份信息（secretId, secretKey）。
        // SECRETID 和 SECRETKEY 请登录访问管理控制台 https://console.cloud.tencent.com/cam/capi 进行查看和管理
        String secretId = "AKIDyjFWcT6VeXAXeV5PUIq8BxTjjTxBBwRg";//用户的 SecretId，建议使用子账号密钥，授权遵循最小权限指引，降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
        String secretKey = "0lGUSAcokQq0uYw4vW0ECuMZVBMJzPdc";//用户的 SecretKey，建议使用子账号密钥，授权遵循最小权限指引，降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        // 2 设置 bucket 的地域, COS 地域的简称请参见 https://cloud.tencent.com/document/product/436/6224
        // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
        Region region = new Region("ap-chengdu");
        ClientConfig clientConfig = new ClientConfig(region);
        // 这里建议设置使用 https 协议
        // 从 5.6.54 版本开始，默认使用了 https
        clientConfig.setHttpProtocol(HttpProtocol.https);
        // 3 生成 cos 客户端。
        return new COSClient(cred, clientConfig);
    }

    /**
     * 阿里翻译
     */
    @Bean
    public Client myClient() throws Exception {
        final String AccessKeyID = "LTAI4G3jcftZYsgZVmTXtH85";
        final String AccessKeySecret = "AkFBLPPONetbTwbqYe0nZDJYpiieUH";
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(AccessKeyID)
                // 您的AccessKey Secret
                .setAccessKeySecret(AccessKeySecret);
        // 访问的域名
        config.endpoint = "mt.aliyuncs.com";
        return new Client(config);
    }
}
